<template>
  <div class="manage">
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose">
      <!--      订单的表单信息-->
      <el-form ref="form" :inline="true" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号"></el-input>
        </el-form-item>
        <el-form-item label="订单金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入订单金额"></el-input>
        </el-form-item>
        <el-form-item label="购买用户" prop="buyer">
          <el-input v-model="form.buyer" placeholder="请输入购买用户"></el-input>
        </el-form-item>
        <el-form-item label="购买时间" prop="purchaseTime">
          <el-date-picker
            type="datetime"
            placeholder="选择日期时间"
            v-model="form.purchaseTime"
            style="width: 100%;"
            value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入订单地址"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <div class="manage-header">
      <el-button type="primary" @click="handleAdd">+新增</el-button>
<!--    form搜索区-->
      <el-form :model="userForm" :inline="true">
        <el-form-item>
          <el-input v-model="userForm.name" placeholder="请输入名称" @keyup.enter="onSubmit" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-table">
      <el-table
          stripe
          height="90%"
          :data="tableData"
          style="width: 100%">
        <el-table-column
            prop="orderNo"
            label="订单编号">
        </el-table-column>
        <el-table-column
            prop="amount"
            label="订单金额">
        </el-table-column>
        <el-table-column
            prop="buyer"
            label="购买用户">
        </el-table-column>
        <el-table-column
            prop="purchaseTime"
            label="购买时间">
        </el-table-column>
        <el-table-column
            prop="address"
            label="订单地址">
        </el-table-column>
        <el-table-column
            prop="action"
            label="操作">
          <!--自定义列，使用插槽的方式，element-ui上面有提供-->
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" @click="handleDelete(scope.row)" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--    分页-->
      <div class="pager">
        <el-pagination
            layout="prev, pager, next"
            :total="total"
            @current-change="handleChange">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Payments",
  data(){
    return {
      dialogVisible:false,
      form: {
        orderNo: '',
        amount: '',
        buyer: '',
        purchaseTime: '',
        address: '',
      },
      rules: {
        orderNo: [{required: true, message: "请输入订单编号"}],
        amount: [{required: true, message: "请输入订单金额"}],
        buyer: [{required: true, message: "请输入购买用户"}],
        purchaseTime: [{required: true, message: "请选择购买时间"}],
        address: [{required: true, message: "请输入订单地址"}],
      },
      tableData: [],
      modalType:0,  
      total:0,   
      pageData: {
        page:1,
        limit:10
      },
      userForm: {
        name:''
      },
      orders: [], // 本地模拟订单数据
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const index = this.tableData.findIndex(item => item.id === this.form.id);
          if (index === -1) {
            // 新增商品
            this.tableData.push({ ...this.form, id: this.tableData.length + 1 });
          } else {
            // 更新商品
            this.tableData.splice(index, 1, { ...this.form });
          }
          this.dialogVisible = false;
          this.$refs.form.resetFields();
          this.form = { id: '', name: '', model: '', price: '', modify: '' }; // 重置表单
          this.updateAllProducts(); // 更新模拟数据
        }
      });
    },
  handleAdd() {
    this.modalType = 0;
    this.dialogVisible = true;
    this.form = { // 初始化表单数据
      orderNo: '',
      amount: '',
      buyer: '',
      purchaseTime: '',
      address: '',
    };
  },
    handleClose() {
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    cancel() {
      this.handleClose();
    },
    handleEdit(row) {
      this.modalType = 1;
      this.dialogVisible = true;
      this.form = JSON.parse(JSON.stringify(row));
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let index = this.orders.findIndex(order => order.id === row.id);
        if (index !== -1) {
          this.orders.splice(index, 1);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList();
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    getList() {
      this.tableData = this.orders.slice((this.pageData.page - 1) * this.pageData.limit, this.pageData.page * this.pageData.limit);
      this.total = this.orders.length;
    },
    handleChange(val) {
      this.pageData.page = val;
      this.getList();
    },
    onSubmit() {
      this.getList();
    },
    handleAdd() {
      this.modalType = 0;
      this.form = {}; // 清空表单
      this.dialogVisible = true;
    },
  },
  mounted() {
    // 模拟数据初始化
    this.updateAllProducts(); // 初始调用更新数据和分页
  }
}
</script>

<style scoped lang="less">
.manage{
  height: 90%;
  .manage-header{
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .common-table{
    position: relative;
    height: calc(100% - 62px);
    .pager{
      position: absolute;
      bottom: 0;
      right: 20px;
    }
  }
}
</style>

